package com.zshan.clinic.admin.service.stat;

import com.zshan.clinic.admin.entity.vo.*;
import com.zshan.clinic.admin.mapper.stat.SpecStatMapper;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.common.util.page.PageUtils;
import com.zshan.clinic.database.dao.*;
import com.zshan.clinic.database.model.*;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 诊所业绩统计
 * @author lichen
 */
@Data
@Service
public class ClinicPerformanceService {


    @Autowired
    private StatClinicDispenseDailyMapper statClinicDispenseDailyMapper;

    @Autowired
    private StatClinicExamDailyMapper statClinicExamDailyMapper;

    @Autowired
    private StatClinicRegDailyMapper statClinicRegDailyMapper;

    @Autowired
    private StatClinicChargeDailyMapper statClinicChargeDailyMapper;

    @Autowired
    private StatClinicMedicalDailyMapper statClinicMedicalDailyMapper;

    @Autowired
    private SpecStatMapper specStatMapper;

    /**
     * 诊所业绩统计
     * @param statDate
     */
    public void clinicPerformanceStat(LocalDate statDate){
        //挂号统计
        List<StatClinicRegDaily> statClinicRegDailyList = specStatMapper.getStatClinicRegDailyList(statDate);
        addStatClinicRegDailyBatch(statClinicRegDailyList);
        //收费统计
        List<StatClinicChargeDaily> statClinicChargeDailyList = specStatMapper.getStatClinicChargeDailyList(statDate);
        addStatClinicChargeDailyBatch(statClinicChargeDailyList);
        //门诊统计
        List<StatClinicMedicalDaily> statClinicMedicalDailyList = specStatMapper.getStatClinicMedicalDailyList(statDate);
        addStatClinicMedicalDailyBatch(statClinicMedicalDailyList);
        //查验统计
        List<StatClinicExamDaily> statClinicExamDailyList = specStatMapper.getStatClinicExamDailyList(statDate);
        addStatClinicExamDailyBatch(statClinicExamDailyList);
        //外治统计
        List<StatClinicExamDaily> statClinicExtDailyList = specStatMapper.getStatClinicExtDailyList(statDate);
        addStatClinicExamDailyBatch(statClinicExtDailyList);
        //药房统计
        List<StatClinicDispenseDaily> statClinicDispenseDailyList = specStatMapper.getStatClinicDispenseDailyList(statDate);
        addStatClinicDispenseDailyBatch(statClinicDispenseDailyList);
    }

    /**
     * 药房统计
     * @param clinicId
     * @param empId
     * @param startDate
     * @param endDate
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageInfo<StatClinicDispenseDailyVo> getEmpDispenseDailyList(Long clinicId, Long empId, Long deptId,LocalDate startDate, LocalDate endDate, Integer pageNo, Integer pageSize){
        StatClinicDispenseDailyExample example = new StatClinicDispenseDailyExample();
        StatClinicDispenseDailyExample.Criteria criteria = example.createCriteria();
        criteria.andClinicIdEqualTo(clinicId);
        if(empId != null){
            criteria.andEmpIdEqualTo(empId);
        }
        if(deptId != null){
            criteria.andDeptIdEqualTo(deptId);
        }
        if(startDate != null){
            criteria.andStatDateGreaterThanOrEqualTo(startDate);
        }
        if(endDate != null){
            criteria.andStatDateLessThanOrEqualTo(endDate);
        }
        long totalCount = statClinicDispenseDailyMapper.countByExample(example);
        if(totalCount == 0){
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo,pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("stat_date desc");
        List<StatClinicDispenseDaily> dispenseDailyList = statClinicDispenseDailyMapper.selectByExample(example);
        return new PageInfo<>(totalCount,dispenseDailyList.stream().map(daily -> {
            StatClinicDispenseDailyVo statClinicDispenseDailyVo = new StatClinicDispenseDailyVo();
            BeanUtils.copyProperties(daily, statClinicDispenseDailyVo);
            return statClinicDispenseDailyVo;
        }).collect(Collectors.toList()));
    }

    /**
     * 门诊统计
     * @param clinicId
     * @param empId
     * @param startDate
     * @param endDate
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageInfo<StatClinicExamDailyVo> getEmpExamDailyList(String projectCategory,Long clinicId,Long empId, Long deptId,LocalDate startDate, LocalDate endDate, Integer pageNo, Integer pageSize){
        StatClinicExamDailyExample example = new StatClinicExamDailyExample();
        StatClinicExamDailyExample.Criteria criteria = example.createCriteria();
        criteria.andClinicIdEqualTo(clinicId);
        criteria.andProjectCategoryEqualTo(projectCategory);
        if(empId != null){
            criteria.andEmpIdEqualTo(empId);
        }
        if(deptId != null){
            criteria.andDeptIdEqualTo(deptId);
        }
        if(startDate != null){
            criteria.andStatDateGreaterThanOrEqualTo(startDate);
        }
        if(endDate != null){
            criteria.andStatDateLessThanOrEqualTo(endDate);
        }
        long totalCount = statClinicExamDailyMapper.countByExample(example);
        if(totalCount == 0){
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo,pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("stat_date desc");
        List<StatClinicExamDaily> examDailyList = statClinicExamDailyMapper.selectByExample(example);
        return new PageInfo<>(totalCount,examDailyList.stream().map(daily -> {
            StatClinicExamDailyVo statClinicExamDailyVo = new StatClinicExamDailyVo();
            BeanUtils.copyProperties(daily, statClinicExamDailyVo);
            return statClinicExamDailyVo;
        }).collect(Collectors.toList()));
    }


    /**
     * 门诊统计
     * @param clinicId
     * @param empId
     * @param startDate
     * @param endDate
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageInfo<StatClinicMedicalDailyVo> getEmpMedicalDailyList(Long clinicId, Long empId, Long deptId,LocalDate startDate, LocalDate endDate, Integer pageNo, Integer pageSize){
        StatClinicMedicalDailyExample example = new StatClinicMedicalDailyExample();
        StatClinicMedicalDailyExample.Criteria criteria = example.createCriteria();
        criteria.andClinicIdEqualTo(clinicId);
        if(empId != null){
            criteria.andEmpIdEqualTo(empId);
        }
        if(deptId != null){
            criteria.andDeptIdEqualTo(deptId);
        }
        if(startDate != null){
            criteria.andStatDateGreaterThanOrEqualTo(startDate);
        }
        if(endDate != null){
            criteria.andStatDateLessThanOrEqualTo(endDate);
        }
        long totalCount = statClinicMedicalDailyMapper.countByExample(example);
        if(totalCount == 0){
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo,pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("stat_date desc");
        List<StatClinicMedicalDaily> medicalDailyList = statClinicMedicalDailyMapper.selectByExample(example);
        return new PageInfo<>(totalCount,medicalDailyList.stream().map(daily -> {
            StatClinicMedicalDailyVo statClinicMedicalDailyVo = new StatClinicMedicalDailyVo();
            BeanUtils.copyProperties(daily, statClinicMedicalDailyVo);
            return statClinicMedicalDailyVo;
        }).collect(Collectors.toList()));
    }

    /**
     * 收费统计
     * @param clinicId
     * @param empId
     * @param startDate
     * @param endDate
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageInfo<StatClinicChargeDailyVo> getEmpChargeDailyList(Long clinicId, Long empId, Long deptId,LocalDate startDate, LocalDate endDate, Integer pageNo, Integer pageSize){
        StatClinicChargeDailyExample example = new StatClinicChargeDailyExample();
        StatClinicChargeDailyExample.Criteria criteria = example.createCriteria();
        criteria.andClinicIdEqualTo(clinicId);
        if(empId != null){
            criteria.andEmpIdEqualTo(empId);
        }
        if(deptId != null){
            criteria.andDeptIdEqualTo(deptId);
        }
        if(startDate != null){
            criteria.andStatDateGreaterThanOrEqualTo(startDate);
        }
        if(endDate != null){
            criteria.andStatDateLessThanOrEqualTo(endDate);
        }
        long totalCount = statClinicChargeDailyMapper.countByExample(example);
        if(totalCount == 0){
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo,pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("stat_date desc");
        List<StatClinicChargeDaily> chargeDailyList = statClinicChargeDailyMapper.selectByExample(example);
        return new PageInfo<>(totalCount,chargeDailyList.stream().map(daily -> {
            StatClinicChargeDailyVo statClinicChargeDailyVo = new StatClinicChargeDailyVo();
            BeanUtils.copyProperties(daily, statClinicChargeDailyVo);
            return statClinicChargeDailyVo;
        }).collect(Collectors.toList()));
    }

    /**
     * 挂号统计
     * @return
     */
    public PageInfo<StatClinicRegDailyVo> getEmpRegDailyList(Long clinicId, Long empId,Long deptId,LocalDate startDate, LocalDate endDate,Integer pageNo,Integer pageSize){
        StatClinicRegDailyExample example = new StatClinicRegDailyExample();
        StatClinicRegDailyExample.Criteria criteria = example.createCriteria();
        criteria.andClinicIdEqualTo(clinicId);
        if(empId != null){
            criteria.andEmpIdEqualTo(empId);
        }
        if(deptId != null){
            criteria.andDeptIdEqualTo(deptId);
        }
        if(startDate != null){
            criteria.andStatDateGreaterThanOrEqualTo(startDate);
        }
        if(endDate != null){
            criteria.andStatDateLessThanOrEqualTo(endDate);
        }
        long totalCount = statClinicRegDailyMapper.countByExample(example);
        if(totalCount == 0){
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo,pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("stat_date desc");
        List<StatClinicRegDaily> regDailyList = statClinicRegDailyMapper.selectByExample(example);
        return new PageInfo<>(totalCount,regDailyList.stream().map(daily -> {
            StatClinicRegDailyVo statClinicRegDailyVo = new StatClinicRegDailyVo();
            BeanUtils.copyProperties(daily, statClinicRegDailyVo);
            return statClinicRegDailyVo;
        }).collect(Collectors.toList()));
    }

    /**
     * 批量添加挂号统计
     * @param statClinicRegDailyList
     */
    public void addStatClinicRegDailyBatch(List<StatClinicRegDaily> statClinicRegDailyList){
        if(CollectionUtils.isEmpty(statClinicRegDailyList)){
            return;
        }
        Date createTime = new Date();
        for(StatClinicRegDaily statClinicRegDaily : statClinicRegDailyList){
            statClinicRegDaily.setCreateTime(createTime);
        }
        specStatMapper.addStatClinicRegDailyBatch(statClinicRegDailyList);
    }

    /**
     * 批量添加收费统计
     * @param statClinicChargeDailyList
     */
    public void addStatClinicChargeDailyBatch(List<StatClinicChargeDaily> statClinicChargeDailyList){
        if(CollectionUtils.isEmpty(statClinicChargeDailyList)){
            return;
        }
        Date createTime = new Date();
        for(StatClinicChargeDaily statClinicChargeDaily : statClinicChargeDailyList){
            statClinicChargeDaily.setCreateTime(createTime);
        }
        specStatMapper.addStatClinicChargeDailyBatch(statClinicChargeDailyList);
    }



    /**
     * 批量添加门诊统计
     * @param statClinicMedicalDailyList
     */
    public void addStatClinicMedicalDailyBatch(List<StatClinicMedicalDaily> statClinicMedicalDailyList){
        if(CollectionUtils.isEmpty(statClinicMedicalDailyList)){
            return;
        }
        Date createTime = new Date();
        for(StatClinicMedicalDaily statClinicMedicalDaily : statClinicMedicalDailyList){
            statClinicMedicalDaily.setCreateTime(createTime);
        }
        specStatMapper.addStatClinicMedicalDailyBatch(statClinicMedicalDailyList);
    }

    /**
     * 批量添加查验统计
     * @param statClinicExamDailyList
     */
    public void addStatClinicExamDailyBatch(List<StatClinicExamDaily> statClinicExamDailyList){
        if(CollectionUtils.isEmpty(statClinicExamDailyList)){
            return;
        }
        Date createTime = new Date();
        for(StatClinicExamDaily statClinicExamDaily : statClinicExamDailyList){
            statClinicExamDaily.setCreateTime(createTime);
        }
        specStatMapper.addStatClinicExamDailyBatch(statClinicExamDailyList);
    }

    /**
     * 批量添加药房统计
     * @param statClinicDispenseDailyList
     */
    public void addStatClinicDispenseDailyBatch(List<StatClinicDispenseDaily> statClinicDispenseDailyList){
        if(CollectionUtils.isEmpty(statClinicDispenseDailyList)){
            return;
        }
        Date createTime = new Date();
        for(StatClinicDispenseDaily statClinicDispenseDaily : statClinicDispenseDailyList){
            statClinicDispenseDaily.setCreateTime(createTime);
        }
        specStatMapper.addStatClinicDispenseDailyBatch(statClinicDispenseDailyList);
    }
}
